package com.csw.shuanfa.utils;

import com.csw.shuanfa.GlobalConfig.exception.BusinessException;
import com.csw.shuanfa.GlobalConfig.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class CommonUtil {
//    private static BaseFileService fileService;
//
//    private static IdWorker idWorker;
//    private static SystemService systemService;
//
//    public BasCommonUtilServiceImpl(BaseFileService fileService, IdWorker idWorker, SystemService systemService) {
//        BasCommonUtilServiceImpl.fileService = fileService;
//        BasCommonUtilServiceImpl.idWorker = idWorker;
//        BasCommonUtilServiceImpl.systemService = systemService;
//    }
//
//    public static void setFiles(List<FileVo> fileVoList, Long detailDescriptionFileId) {
//
//        if (CollectionUtils.isNotEmpty(fileVoList)) {
//            List<BaseFile> list = BeanUtil.copyToList(fileVoList, BaseFile.class);
//            list.stream().forEach(baseFile -> {
//                baseFile.setId(idWorker.nextId());
//                baseFile.setRefId(detailDescriptionFileId);
//                if (baseFile.getFileName() == null) {
//                    baseFile.setFileName("");
//                }
//            });
//            fileService.saveBatch(list);
//        }
//    }
//
//    public static List<FileVo> getFileVos(Long refId) {
//        List<FileVo> refIdList = fileService.listByRefId(refId);
//        return refIdList;
//    }

    /**
     * 按照竖线分隔
     *
     * @param str  多个拼接
     * @param list
     * @return
     */
    public static String toValueArrayString(String str, List<T> list) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        if (str.startsWith("|")) {
            str = str.substring(1);
        }
        if (str.endsWith("|")) {
            str = str.substring(0, str.length() - 1);
        }
        String[] split = str.split("\\|");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String aa = split[i];
//            if (i != split.length - 1) {
//                stringBuilder.append(toValueString(aa, list) + "|");
//            } else {
//                stringBuilder.append(toValueString(aa, list));
//            }
        }
        return stringBuilder.toString();

    }

    /**
     * 按照英文逗号分隔
     *
     * @param str  多个拼接
     * @param list
     * @return
     */
    public static String toValueArrayStringComma(String str, List<T> list) {
        if (StringUtils.isEmpty(str)) {
            return str;
        }
        if (str.startsWith(",")) {
            str = str.substring(1);
        }
        if (str.endsWith(",")) {
            str = str.substring(0, str.length() - 1);
        }
        String[] split = str.split(",");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            String aa = split[i];
//            if (i != split.length - 1) {
//                stringBuilder.append(toValueString(aa, list) + ",");
//            } else {
//                stringBuilder.append(toValueString(aa, list));
//            }
        }
        return stringBuilder.toString();

    }

    /**
     * @param primaryClassification 单个
     * @param list
     * @return
     */
//    public static String toValueString(String primaryClassification, List<T> list) {
//        if (list != null) {
//            for (DictItemRespVo data : list) {
//                if (data.getItemValue().equals(primaryClassification)) {
//                    return data.getItemText();
//                }
//            }
//        }
//        return primaryClassification;
//    }
    public static void judgeTime(String startTime, String endTime) {
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isEmpty(endTime)) {
            throw new BusinessException("开始时间和结束时间必须都传!");
        }
        if (StringUtils.isEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            throw new BusinessException("开始时间和结束时间必须都传!");
        }
        if (StringUtils.isNotEmpty(startTime)) {
            Date getStartTime = null;
            Date getEndTime = null;
            try {
                getStartTime = TimeUtil.sdf.parse(startTime);
                getEndTime = TimeUtil.sdf.parse(endTime);
            } catch (ParseException e) {
                throw new BusinessException("开始时间或者结束时间格式不对");
            }
            //结束时间必须大于开始时间
            if (getEndTime.compareTo(getStartTime) <= 0) {
                throw new BusinessException("结束时间必须大于开始时间");
            }
        }
    }

    public static void judgeTimeCycle(String startTime, String endTime) {
        Date getStartTime = null;
        Date getEndTime = null;
        try {
            getStartTime = TimeUtil.sdf.parse(startTime);
            getEndTime = TimeUtil.sdf.parse(endTime);
        } catch (ParseException e) {
            throw new BusinessException("开始时间或者结束时间格式不对");
        }
        //开始时间必须大于等于当前时间
        if (getStartTime.compareTo(new Date()) <= 0) {
            throw new BusinessException("开始时间必须大于当前时间");
        }
        //结束时间必须大于开始时间
        if (getEndTime.compareTo(getStartTime) <= 0) {
            throw new BusinessException("结束时间必须大于开始时间");
        }
    }

    public static void judgeTimeCycleList(String startTime, String endTime) {
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isEmpty(endTime)) {
            throw new BusinessException("开始时间和结束时间必须都传!");
        }
        if (StringUtils.isEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            throw new BusinessException("开始时间和结束时间必须都传!");
        }
        if (StringUtils.isNotEmpty(startTime)) {
            Date getStartTime = null;
            Date getEndTime = null;
            try {
                getStartTime = TimeUtil.sdf.parse(startTime);
                getEndTime = TimeUtil.sdf.parse(endTime);
            } catch (ParseException e) {
                throw new BusinessException("开始时间或者结束时间格式不对");
            }
            //结束时间必须大于开始时间
            if (getEndTime.compareTo(getStartTime) <= 0) {
                throw new BusinessException("结束时间必须大于开始时间");
            }
        }
    }

    public static Result<Boolean> getBooleanResult(boolean b) {
        if (b == true) {
            return Result.ok(b);
        } else {
            return Result.err();
        }
    }

//    public static List<DictItemRespVo> respVoList(String dict) {
//        Result<List<DictItemRespVo>> listResult = systemService.getAllDictItemByDictCode(dict);
//        if (listResult.getRespCode() != ErrCode.OK || listResult.getDatas() == null) {
//            throw new BusinessException(Constant.SYSTEM_SERVICE_ERROR);
//        }
//        return listResult.getDatas();
//    }

    /**
     * 英文转中文
     */
//    public static String toMazhi(String placeName, List<DictItemRespVo> list) {
//        if (list != null) {
//            for (DictItemRespVo data : list) {
//                if (data.getItemText().equals(placeName)) {
//                    return data.getItemValue();
//                }
//            }
//        }
//        throw new BusinessException("码值匹配出现问题");
//    }
}
